home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / QuickDraw3D 1.6 SDK / Mac SampleCode Previous / Geometry Samples- Mac / GeometryTest / GTShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  12.5 KB  |  527 lines  |  [TEXT/MPCC]

  1. // Metafile read - shows how to read metafile data.
  2. //
  3. // Nick Thompson - January 6th 1994
  4. //
  5. // © 1995, Apple Computer Inc., All Rights Reserved
  6.  
  7. // system headers
  8. #include <Devices.h>
  9. #include <Dialogs.h>
  10. #include <DiskInit.h>
  11. #include <Fonts.h>
  12. #include <Menus.h>
  13. #include <PictUtils.h>
  14. #include <QDOffScreen.h>
  15. #include <QuickDraw.h>
  16. #include <SegLoad.h>
  17. #include <StandardFile.h>
  18. #include <TextEdit.h>
  19. #include <ToolUtils.h>
  20.  
  21. // for QuickDraw 3D
  22. #include "QD3D.h"
  23. #include "QD3DMath.h"
  24. #include "QD3DDrawContext.h"
  25. #include "QD3DShader.h"
  26. #include "QD3DTransform.h"
  27. #include "QD3DGroup.h"
  28. #include "QD3DCamera.h"
  29.  
  30. #include "GTFile.h"
  31. #include "GTShell.h"
  32. #include "GTSupport.h"
  33.  
  34. #include "Geometries.h"
  35. #include "MyErrorHandler.h"
  36. #include "Error_Lib.h"
  37.  
  38. //-------------------------------------------------------------------------------------------
  39. // function prototypes
  40.  
  41. static void         InitToolbox( void ) ;
  42. static void         MainEventLoop( void ) ;
  43. static void         HandleKeyPress(EventRecord *event) ;
  44. static void         HandleOSEvent(EventRecord *event) ;
  45. static TQ3Status     DocumentDraw3DData( DocumentHdl theDocument ) ;
  46. static Boolean         IsAppWindow(WindowPtr window);
  47.  
  48. //-------------------------------------------------------------------------------------------
  49. //
  50.  
  51. Boolean         gQuitFlag         = false ;
  52.  
  53. //-------------------------------------------------------------------------------------------
  54. // main()
  55. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  56. // and enter the main event loop.  On exit from the main event loop, we want to call
  57. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  58.  
  59. void main(void)
  60. {
  61.     TQ3Status    myStatus;
  62.     Rect        rBounds = { 50, 50, 350, 350 } ;
  63.     Str255        title = "\pSpinning Box" ;
  64.  
  65.     InitToolbox() ;
  66.         
  67.     //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  68.     myStatus = Q3Initialize();
  69.     if ( myStatus == kQ3Failure ) {
  70.         DebugStr("\pErInitialize returned failure.");    
  71.         ExitToShell() ;
  72.     }
  73.                     
  74.     // install the error handler - this gets called whenever
  75.     // an error occurs, which means we don't have to check so 
  76.     // much
  77.     InstallDefaultErrorHandler();
  78.     InstallDefaultWarningHandler();
  79.     InstallDefaultNoticeHandler();
  80.  
  81.     // set up our globals
  82.     gQuitFlag = false ;
  83.     
  84.     // loop till we doop
  85.     MainEventLoop();
  86.     
  87.     //    Close our connection to the QuickDraw 3D library
  88.     myStatus = Q3Exit();
  89.     if ( myStatus == kQ3Failure )
  90.         DebugStr("\pErExit returned failure.");
  91. }    
  92.  
  93.  
  94. //-----------------------------------------------------------------------------
  95. // assumes the port is set up before being called
  96.  
  97. TQ3Status DocumentDraw3DData( DocumentHdl theDocument )
  98. {
  99. //{
  100. //    TQ3CameraData        theCameraData ;
  101. //    TQ3CameraObject        theCameraObject;
  102. //    
  103. //    Q3View_GetCamera(theDocument->fView, &theCameraObject);
  104. //    Q3Camera_GetData(theCameraObject, &theCameraData);
  105. //Debugger() ;
  106. //
  107. //}
  108.     TQ3Status theStatus ;
  109.         
  110.     if((**theDocument).fModel != nil ) {
  111.         HLock((Handle)theDocument );
  112.         theStatus = Q3View_StartRendering((**theDocument).fView) ;
  113.         do {
  114.             theStatus = SubmitScene( theDocument ) ;
  115.         } while (Q3View_EndRendering((**theDocument).fView) == kQ3ViewStatusRetraverse );
  116.     
  117.         HUnlock((Handle)theDocument );
  118.     }    
  119.     return theStatus ;    
  120.  
  121.     
  122. }
  123.  
  124.  
  125. //----------------------------------------------------------------------------------
  126.  
  127. //-------------------------------------------------------------------------------------------
  128. //
  129.  
  130. short HiWrd(long aLong)
  131. {
  132.     return    (((aLong) >> 16) & 0xFFFF) ;
  133. }
  134.  
  135. //-------------------------------------------------------------------------------------------
  136. //
  137.  
  138. short LoWrd(long aLong)
  139. {
  140.     return    ((aLong) & 0xFFFF) ;
  141.  
  142. }
  143. //-------------------------------------------------------------------------------------------
  144. //    IsAppWindow
  145. //
  146. //    Check to see if a window belongs to the application. If the window pointer
  147. //    passed was NIL, then it could not be an application window. WindowKinds
  148. //    that are negative belong to the system and windowKinds less than userKind
  149. //    are reserved by Apple except for windowKinds equal to dialogKind, which
  150. //    mean it is a dialog.
  151.  
  152. static Boolean IsAppWindow(WindowPtr window)
  153. {
  154.     short        windowKind;
  155.  
  156.     if ( window == nil )
  157.         return false;
  158.     else {
  159.         windowKind = ((WindowPeek) window)->windowKind;
  160.         return ((windowKind >= userKind) || (windowKind == dialogKind));
  161.     }
  162. }
  163.  
  164.  
  165. //-------------------------------------------------------------------------------------------
  166. //    IsDAWindow
  167. //
  168. //    Check to see if a window belongs to a desk accessory. It belongs to a DA
  169. //    if the windowKind field of the window record is negative.
  170.  
  171. static Boolean IsDAWindow(WindowPtr window)
  172. {
  173.     if ( window == nil )
  174.         return false;
  175.     else
  176.         return ( ((WindowPeek) window)->windowKind < 0 );
  177. }
  178.  
  179.  
  180. //-------------------------------------------------------------------------------------------
  181. //    IsDialogWindow
  182. //
  183. //    Check to see if a window is a dialog window. We can determine this be
  184. //    checking to see if the windowKind field is equal to dialogKind.
  185.  
  186. static Boolean IsDialogWindow(WindowPtr window)
  187. {
  188.     if ( window == nil )
  189.         return false;
  190.     else
  191.         return ( ((WindowPeek) window)->windowKind == dialogKind );
  192. }
  193.  
  194.  
  195. //-------------------------------------------------------------------------------------------
  196. //
  197. static void AdjustMenus()
  198. {
  199.     WindowPtr    window;
  200.     MenuHandle    menu;
  201.  
  202.     window = FrontWindow();
  203.  
  204.     menu = GetMHandle(mFile);
  205.     
  206.     if ( window != nil )
  207.         EnableItem(menu, iClose);
  208.     else
  209.         DisableItem(menu, iClose);
  210.  
  211.     menu = GetMHandle(mEdit);
  212.     if ( IsDAWindow(window) ) {
  213.         EnableItem(menu, iUndo);
  214.         EnableItem(menu, iCut);
  215.         EnableItem(menu, iCopy);
  216.         EnableItem(menu, iClear);
  217.         EnableItem(menu, iPaste);
  218.     } else {                        
  219.         DisableItem(menu, iUndo);
  220.         DisableItem(menu, iCut);
  221.         DisableItem(menu, iCopy);
  222.         DisableItem(menu, iClear);
  223.         DisableItem(menu, iPaste);
  224.     }
  225. }
  226.  
  227. //-------------------------------------------------------------------------------------------
  228. //
  229. void InitToolbox()
  230. {
  231.     Handle        menuBar;
  232.     EventRecord event;
  233.     short        count;
  234.  
  235.     // initialize application globals
  236.     gQuitFlag = false;
  237.     
  238.     MaxApplZone() ;
  239.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  240.     
  241.     // Init the managers
  242.     InitGraf( &qd.thePort );
  243.     InitFonts();
  244.     InitWindows();
  245.     InitMenus();
  246.     TEInit();
  247.     InitDialogs(nil);
  248.  
  249.  
  250.     FlushEvents( everyEvent, 0 ) ;
  251.         
  252.     //    This next bit of code waits until the Finder brings our application
  253.     //    to the front. This gives us a better effect if we open a window at
  254.     //    startup.
  255.  
  256.     for (count = 1; count <= 3; ++count)
  257.         EventAvail(everyEvent, &event);
  258.  
  259.     menuBar = GetNewMBar( 128 );
  260.     if ( menuBar == nil )
  261.          ExitToShell() ;
  262.          
  263.     SetMenuBar( menuBar );
  264.     DisposHandle( menuBar );
  265.     AddResMenu( GetMHandle(mApple), 'DRVR' );    // Add DA names to Apple menu
  266.     AdjustMenus();
  267.     DrawMenuBar();
  268.     InitCursor();                                // reset the cursor
  269. }
  270.  
  271. //-------------------------------------------------------------------------------------------
  272. //    HandleMenuCommand
  273. //
  274. //    This is called when an item is chosen from the menu bar (after calling
  275. //    MenuSelect or MenuKey). It performs the right operation for each command.
  276. //    It is good to have both the result of MenuSelect and MenuKey go to one
  277. //    routine like this to keep everything organized.
  278.  
  279. static void HandleMenuCommand(long    menuResult)
  280. {
  281.     short        menuID;                // Resource ID# of the selected menu
  282.     short        menuItem;            // Item number of the selected menu
  283.     Str255        daName;
  284.     DialogPtr    theDialog ; 
  285.     short        itemHit ;
  286.     WindowPtr    theWindow ;
  287.  
  288.     menuID = HiWrd(menuResult);
  289.     menuItem = LoWrd(menuResult);
  290.     switch ( menuID ) {
  291.         case mApple:
  292.             switch ( menuItem ) {
  293.                 ModalFilterUPP         theProc ;
  294.  
  295.                 case iAbout:
  296.                 
  297.                     theDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  298.                     
  299.                     // these two lil' snappers are system 7 only
  300.                     // so if you use them, check before!!
  301.                     GetStdFilterProc( &theProc ) ;
  302.                     SetDialogDefaultItem(theDialog, ok) ;
  303.                     
  304.                     
  305.                     do {
  306.                         ModalDialog ( theProc, &itemHit );
  307.                     } while( itemHit != ok ) ;
  308.                     DisposDialog ( theDialog );
  309.                     break;
  310.  
  311.                 default:            // All non-About items in this menu are DAs
  312.                     GetItem(GetMHandle(mApple), menuItem, daName);
  313.                     (void) OpenDeskAcc(daName);
  314.                     break;
  315.             }
  316.             break;
  317.         case mFile:
  318.             switch ( menuItem ) {
  319.                 case iNew:
  320.                     HandleNewCommand() ;
  321.                     break;
  322.                 case iOpen:
  323.                     HandleOpenCommand() ;
  324.                     break;
  325.                 case iClose:
  326.                     HandleCloseWindow( FrontWindow() ) ;
  327.                     break;
  328.                 case iQuit:
  329.                     gQuitFlag = true;
  330.                     break;
  331.             }
  332.             break;
  333.         case mEdit:
  334.             switch (menuItem) {
  335.                 // Call SystemEdit for DA editing & MultiFinder 
  336.                 // since we don’t do any Editing yet
  337.                 case iUndo:
  338.                 case iCut:
  339.                 case iCopy:
  340.                 case iPaste:
  341.                 case iClear:
  342.                     (void) SystemEdit(menuItem-1);
  343.                     break;
  344.             }
  345.             break;
  346.             
  347.         case mGeometry:
  348.             theWindow = FrontWindow() ;
  349.             if (theWindow == NULL){
  350.                 HandleNewCommand();
  351.             }
  352.  
  353.             theWindow = FrontWindow() ;
  354.             if( theWindow ) {
  355.                 DocumentHdl theDocument = (DocumentHdl) GetWRefCon( theWindow ) ;
  356.                 if( theDocument != nil ) {
  357.                     TQ3Point3D        myOrigin = { 0, 0, 0 } ;
  358.                 
  359.                     HLock( (Handle)theDocument ) ;
  360.                     
  361.                     if((**theDocument).fModel != nil) {
  362.                         Q3Object_Dispose((**theDocument).fModel) ;                
  363.                         (**theDocument).fModel = nil ;
  364.                     }
  365.                     
  366.                     (**theDocument).fModel = BuildGeometry(menuItem) ;
  367.                     
  368.                     (**theDocument).fGroupScale = 1;                
  369.                     (**theDocument).fGroupCenter = myOrigin ;            
  370.                     AdjustCamera(    theDocument,
  371.                                     (theWindow->portRect.right - theWindow->portRect.left),
  372.                                     (theWindow->portRect.bottom - theWindow->portRect.top) ) ;
  373.                                     
  374.                     HUnlock((Handle)theDocument);
  375.                 }
  376.                 SetPort( (GrafPtr)theWindow ) ;
  377.                 InvalRect( & theWindow->portRect ) ;
  378.             }
  379.             break ;
  380.     }
  381.     HiliteMenu(0);        // Unhighlight what MenuSelect or MenuKey hilited
  382. }
  383.  
  384. //-------------------------------------------------------------------------------------------
  385. //
  386. void MainEventLoop()
  387. {
  388.     EventRecord     event;
  389.     WindowPtr       window;
  390.     short           thePart;
  391.     Rect            screenRect, updateRect;
  392.     Point            aPoint = {100, 100};
  393.     CGrafPtr        savedPort ;
  394.     
  395.     DocumentHdl        theDocument ;
  396.     
  397.  
  398.     while( !gQuitFlag )
  399.     {
  400.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  401.         {
  402.  
  403.             switch (event.what) {
  404.                 case mouseDown:
  405.                 
  406.                     thePart = FindWindow( event.where, &window );
  407.                     
  408.                     switch( thePart ) {
  409.                         case inMenuBar: 
  410.                             AdjustMenus();
  411.                             HandleMenuCommand(MenuSelect(event.where));
  412.                             break;
  413.                         
  414.                         case inDrag:
  415.                     
  416.                             screenRect = (**GetGrayRgn()).rgnBBox;
  417.                             DragWindow( window, event.where, &screenRect );
  418.                             break ;
  419.                     
  420.                         case inContent:
  421.                     
  422.                             if (window != FrontWindow())
  423.                                 SelectWindow( window );
  424.                             break ;
  425.                     
  426.                         case inGoAway:
  427.                             if (TrackGoAway( window, event.where )) {
  428.                                 HandleCloseWindow( window ) ;
  429.                             }
  430.                             break ;
  431.                             
  432.                         default:
  433.                             break ;
  434.                     }
  435.                     break ;
  436.                             
  437.                         
  438.                 case updateEvt:
  439.                 
  440.                     window = (WindowPtr)event.message;
  441.                     theDocument = (DocumentHdl) GetWRefCon( window ) ;
  442.                     
  443.                     if( theDocument != nil ) {
  444.                         updateRect = (**(window->visRgn)).rgnBBox;
  445.                         
  446.                         GetPort( (GrafPtr *)&savedPort) ;
  447.                         SetPort( window ) ;
  448.                         BeginUpdate( window );
  449.                         DocumentDraw3DData( theDocument ) ;
  450.                         EndUpdate( window );
  451.                         SetPort( (GrafPtr)savedPort ) ;
  452.                     }
  453.  
  454.                     break ;
  455.                     
  456.                 case keyDown:
  457.                 case autoKey:
  458.                     HandleKeyPress(&event);
  459.                     break;
  460.                     
  461.                 case diskEvt:
  462.                     if ( HiWrd(event.message) != noErr ) 
  463.                         (void) DIBadMount(aPoint, event.message);
  464.                     break;
  465.                     
  466.                 case osEvt:
  467.                 case activateEvt:
  468.                     break;
  469.  
  470.  
  471.             }
  472.         }
  473.         else {
  474.             
  475.             window = FrontWindow() ;
  476.             if( window ) {
  477.                 if(IsAppWindow(window)){
  478.                     theDocument = (DocumentHdl) GetWRefCon( window ) ;
  479.                     if( theDocument != nil ) {
  480.  
  481.                         // we received a null event, rotate the object
  482.                         TQ3Matrix4x4    tmp;
  483.                         Rect        theRect = (window)->portRect ;
  484.                         
  485.                         SetPort(window) ;
  486.                         Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.1, 0.12, 0.08);
  487.                         HLock( (Handle)theDocument ) ;
  488.                         Q3Matrix4x4_Multiply(&(**theDocument).fRotation, &tmp, &(**theDocument).fRotation);
  489.                         HUnlock( (Handle)theDocument ) ;
  490.             
  491.                         InvalRect( &theRect ) ;
  492.                     }
  493.                 }
  494.             }
  495.         }
  496.     }
  497.  
  498.     /* Close all windows */
  499.     window = FrontWindow() ;
  500.     while (window != nil) {
  501.         HandleCloseWindow( window ) ;
  502.         window = FrontWindow() ;
  503.     }
  504. }
  505.  
  506.  
  507. //-------------------------------------------------------------------------------------------
  508. //
  509. void HandleKeyPress(EventRecord *event)
  510. {
  511.     char    key;
  512.  
  513.     key = event->message & charCodeMask;
  514.     if ( event->modifiers & cmdKey ) {        /* Command key down? */
  515.         AdjustMenus();                        /* Enable/disable/check menu items properly */
  516.         HandleMenuCommand(MenuKey(key));
  517.     } else {
  518.         /* DoKeyPress(event) */;
  519.     }
  520. }
  521.  
  522. //-------------------------------------------------------------------------------------------
  523. //
  524.  
  525.  
  526.  
  527.